package site.marqstree.latte.ec.main.cart;

import android.graphics.Color;
import android.view.View;
import android.widget.Toast;

import androidx.core.content.ContextCompat;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.joanzapata.iconify.widget.IconTextView;

import java.util.ArrayList;
import java.util.List;

import site.marqstree.latte.core.app.Latte;
import site.marqstree.latte.ec.R;
import site.marqstree.latte.ui.recycler.MultipleItemEntity;

/*
 * 项目名: EleCommerce
 * 包名: site.marqstree.latte.ec.main.cart
 * 文件名: ItemChildClickListener
 * 创建者: marqstree
 * 创建时间: 2020/2/2 9:10
 * 描述: TODO
 */
public class ItemChildClickListener implements OnItemChildClickListener {

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {

        ArrayList<MultipleItemEntity> dataList = (ArrayList<MultipleItemEntity>)adapter.getData();
        ShopCartAdapter shopCartAdapter = (ShopCartAdapter) adapter;
        MultipleItemEntity entity = dataList.get(position);
        //当前行选中状态
        final boolean currentSelected = entity.getField(ShopCartItemFields.IS_SELECTED);
        //勾选框控件
        IconTextView iconIsSelected = (IconTextView) view;
        //当前行数量
        int currentCount = entity.getField(ShopCartItemFields.COUNT);
        //当前行价格
        final double currentPrice = entity.getField(ShopCartItemFields.PRICE);

        //勾选框点击事件
        if (view.getId() == R.id.icon_item_shop_cart) {
            if (currentSelected) {
                entity.setField(ShopCartItemFields.IS_SELECTED, false);
            } else {
                entity.setField(ShopCartItemFields.IS_SELECTED, true);
            }
            adapter.notifyItemChanged(position,ShopCartAdapter.ITEM_SELECTED_PAYLOAD);
        }
        //增加按钮
        else if(view.getId() == R.id.icon_item_plus){
            currentCount++;
            entity.setField(ShopCartItemFields.COUNT, currentCount);
            shopCartAdapter.calTotalPrice();
            adapter.notifyItemChanged(position, ShopCartAdapter.ITEM_COUNT_PAYLOAD);
        }
        //减少按钮
        else if(view.getId() == R.id.icon_item_minus){
            currentCount--;
            entity.setField(ShopCartItemFields.COUNT, currentCount);
            shopCartAdapter.calTotalPrice();
            adapter.notifyItemChanged(position, ShopCartAdapter.ITEM_COUNT_PAYLOAD);
        }
    }

}
